package com.example.directorydemo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String TABLE = "information";
    private static final String TAG = "MainActivity";
    //3 定义界面控件变量
    private EditText mEtName,mEtPhone;
    private Button mBtnAdd,mBtnQuery,mBtnUpdate,mBtnDelete;
    private TextView mTvShow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1 界面初始化
        init();
        // 2事件初始化
        initEvent();
    }

    private void initEvent() {
        mBtnAdd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnUpdate.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);

    }

    private void init() {
        //2
        // 4 绑定控件
        mEtName = findViewById(R.id.et_name);
        mEtPhone = findViewById(R.id.et_phone);
        mBtnAdd = findViewById(R.id.btn_add);
        mBtnDelete = findViewById(R.id.btn_delete);
        mBtnQuery = findViewById(R.id.btn_query);
        mBtnUpdate = findViewById(R.id.btn_update);
        mTvShow = findViewById(R.id.tv_show);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_add:
                add();
                break;
            case R.id.btn_update:
                break;
            case R.id.btn_delete:
                break;
            case R.id.btn_query:
                queryAll();
                break;
        }
    }

    private void queryAll() {
        MyHelper helper = new MyHelper(this,
                "itcast.db",
                null,1);
        SQLiteDatabase db = helper.getReadableDatabase();

        Cursor cursor = db.query(TABLE, null, null,
                null, null, null, null);
        // TODO 将数据保存，并显示到tv_show中
        while (cursor.moveToNext()){

        }
        db.close();
        Log.e(TAG, "add: "+"OK");

    }

    // 添加记录
    private void add() {
        MyHelper helper = new MyHelper(this,
                "itcast.db",
                null,1);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",mEtName.getText().toString());
        values.put("phone",mEtPhone.getText().toString());
        db.insert(TABLE,null,values);
        db.close();
        Log.e(TAG, "add: "+"OK");
    }
    class MyHelper extends SQLiteOpenHelper{

        public MyHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("create table information(" +
                    " _id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    " name VARCHAR(20)," +
                    " phone VARCHAR(11) )");
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }
}